﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace BookstoreDesktopClient
{
    public partial class AddBook : Form
    {
        BookstoreClient bookstoreClient;
        BookWS.BookdbWS booksCollection;
        string sessionID;
        List<TextBox> authorNameTextbox;
        List<TextBox> authorSurnameTextBox;
        int authorNameX = 21;
        int authorNameY = 218;
        int authorSurnameX = 180;
        int authorSurnameY = 218;
        int tabIndex = 100;


        public AddBook(BookstoreClient bookstoreClient, BookWS.BookdbWS booksCollection, string sessionID)
        {
            this.bookstoreClient = bookstoreClient;
            this.booksCollection = booksCollection;
            this.sessionID = sessionID;
            this.authorNameTextbox = new List<TextBox>();
            this.authorSurnameTextBox = new List<TextBox>();
            InitializeComponent();
        }

        private void addBookButton_Click(object sender, EventArgs e)
        {

            BookWS.author[] atab = new BookWS.author[authorNameTextbox.Count];
            BookWS.author a;
            
            try{
                for (int i = 0; i < authorNameTextbox.Count; i++)
                {
                    a = new BookWS.author();
                    a.name = authorNameTextbox[i].Text;
                    a.surname = authorSurnameTextBox[i].Text;
                    atab[i] = a;
                }

                BookWS.publisher p = new BookWS.publisher();
                p.name = publisherTextBox.Text;

                BookWS.book book = new BookWS.book();
                book.title = titleTextBox.Text;
                book.price = System.Convert.ToDouble(priceTextBox.Text);
                book.amount = System.Convert.ToInt32(amountTextBox.Text);
                book.isbn = isbnTextBox.Text;
                book.publisher = p;
                book.authors = atab;
           
                booksCollection.addbookAsync(book,sessionID);
                booksCollection.addbookCompleted += new BookWS.addbookCompletedEventHandler(booksCollection_addbookCompleted);
            }
            catch (Exception exc)
            {
                MessageBox.Show("An error occured: \n" + exc.ToString());
            }
            
            this.Dispose(false);
           
        }

        void booksCollection_addbookCompleted(object sender, BookWS.addbookCompletedEventArgs e)
        {
            bookstoreClient.showAllBooksButton_Click(sender, e);
            if (e.@return) bookstoreClient.sumLabel.Text = "New book has been added.";
            else bookstoreClient.sumLabel.Text = "ERROR: User has NOT been added, because given book exists in database!";
        }

        private void addFieldsButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.TextBox nameTemp = new TextBox();
            nameTemp.Location = new System.Drawing.Point(authorNameX, authorNameY);
            nameTemp.Name = "Author name";
            nameTemp.Size = new System.Drawing.Size(150, 20);
            nameTemp.TabIndex = tabIndex;
            nameTemp.Text = "name";
            authorNameY += 25;
            tabIndex++;
            this.Controls.Add(nameTemp);
            this.authorNameTextbox.Add(nameTemp);

            System.Windows.Forms.TextBox surnameTemp = new TextBox();
            surnameTemp.Location = new System.Drawing.Point(authorSurnameX, authorSurnameY);
            surnameTemp.Name = "Author surname";
            surnameTemp.Size = new System.Drawing.Size(150, 20);
            surnameTemp.TabIndex = tabIndex;
            surnameTemp.Text = "surname";
            authorSurnameY += 25;
            tabIndex++;
            this.Controls.Add(surnameTemp);
            this.authorSurnameTextBox.Add(surnameTemp);
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (authorSurnameTextBox.Count > 0)
            {
                System.Windows.Forms.TextBox nameTemp = authorNameTextbox[authorNameTextbox.Count - 1];
                authorNameTextbox.Remove(nameTemp);
                this.Controls.Remove(nameTemp);

                System.Windows.Forms.TextBox surnameTemp = authorSurnameTextBox[authorSurnameTextBox.Count - 1];
                authorSurnameTextBox.Remove(surnameTemp);
                this.Controls.Remove(surnameTemp);
                tabIndex--;
                authorNameY -= 25;
                authorSurnameY -= 25;
            }
        }
    }
}
